home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / usr / sybase / sample / dblibrary / example3.c < prev    next >
C/C++ Source or Header  |  1993-04-22  |  5KB  |  185 lines

  1. /*
  2. **    example3.c
  3. **
  4. ** This example selects some information from the "pubs" database.
  5. ** It illustrates binding of both aggregate and compute results.
  6. **
  7. ** Note that this example will only work if the "pubs" database exists
  8. ** on your SQL Server. Consult the Installation Guide for information
  9. ** about installing the "pubs" database.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <sybfront.h>
  14. #include <sybdb.h>
  15.  
  16. #define PLEN 4 
  17. #define DATEPRINT 26
  18. #define MONEYPRINT 12
  19.  
  20. /* Forward declarations of the error handler and message handler. */
  21. int              err_handler();
  22. int              msg_handler();
  23.  
  24. main(argc,argv)
  25. int              argc;
  26. char             *argv[];
  27. {
  28.  
  29.     LOGINREC       *login;
  30.     DBPROCESS      *dbproc;
  31.  
  32.     /* Declare the datatypes for the columns in the table "titles". */
  33.  
  34.     DBINT          pcount;
  35.     DBINT          sales;
  36.     DBINT          salesavg;
  37.     DBINT          sumsale;
  38.     DBCHAR         date[DATEPRINT+1];
  39.     DBCHAR         price[MONEYPRINT+1];
  40.     DBCHAR         priceavg[MONEYPRINT+1];
  41.     DBCHAR         pubid[PLEN+1];
  42.     RETCODE        result_code; /* to hold the results of dbresults(). */
  43.     STATUS         row_code;    /* to hold the results of dbnextrow(). */
  44.  
  45.     /* Initialize DB-Library. */
  46.     if (dbinit() == FAIL)
  47.         exit(ERREXIT);
  48.  
  49.     /* Install the user-supplied error-handling and message-handling
  50.      * routines. They are defined at the bottom of this source file.
  51.      */
  52.     dberrhandle(err_handler);
  53.     dbmsghandle(msg_handler);
  54.  
  55.     /* Set up the login information. */
  56.  
  57.     login = dblogin();
  58.     DBSETLPWD(login, "server_password");
  59.     DBSETLAPP(login, "example3");
  60.  
  61.     dbproc = dbopen(login, NULL);
  62.  
  63.     /* Send a "use database" command. */
  64.  
  65.     dbuse(dbproc,"pubs");
  66.  
  67.     /* Put the SQL statement into the command buffer. */
  68.  
  69.     dbcmd(dbproc, "select pub_id, pubdate, price, avg(price), ytd_sales,");
  70.     dbcmd(dbproc, " avg(ytd_sales), sum(ytd_sales) from titles");
  71.     dbcmd(dbproc, " group by pub_id");
  72.     dbcmd(dbproc, " order by pub_id");
  73.     dbcmd(dbproc, " compute count(pub_id) by pub_id");
  74.  
  75.     /* Send the command buffer to SQL Server for execution. */
  76.  
  77.     dbsqlexec(dbproc);
  78.  
  79.     /*
  80.     ** Using the aggregates "sum" and "avg" with the COMPUTE clause 
  81.     ** necessitates special handling when binding the results. Since each
  82.     ** aggregate creates a new column, this is accounted for in the bind.
  83.     ** Notice that avg(price) is the fourth column in the select-list,
  84.     ** and is also specified as the fourth column in the dbbind() routine.
  85.     **
  86.     ** The COMPUTE clause creates a compute row, which requires a 
  87.     ** special bind routine called dbaltbind().
  88.     */
  89.  
  90.     while ((result_code = dbresults(dbproc)) != NO_MORE_RESULTS)
  91.     {
  92.         if (result_code == SUCCEED)
  93.         {
  94.             dbbind(dbproc, 1, NTBSTRINGBIND, (DBINT)0, pubid);
  95.             dbbind(dbproc, 2, NTBSTRINGBIND, (DBINT)0, date);
  96.             dbbind(dbproc, 3, NTBSTRINGBIND, (DBINT)0, price);
  97.             dbbind(dbproc, 4, NTBSTRINGBIND, (DBINT)0, priceavg);
  98.             dbbind(dbproc, 5, INTBIND, (DBINT)0, (BYTE *)&sales);
  99.             dbbind(dbproc, 6, INTBIND, (DBINT)0, (BYTE *)&salesavg);
  100.             dbbind(dbproc, 7, INTBIND, (DBINT)0, (BYTE *)&sumsale);
  101.  
  102.             /* dbaltbind() binds compute columns. */
  103.             dbaltbind(dbproc, 1, 1, INTBIND, (DBINT)0, (BYTE *)&pcount);
  104.  
  105.             printf("\nAccounts:\n");
  106.             printf("---------\n\n");
  107.             printf
  108.                 ("%-5s  %-19s  %-6s  %-10s  %-5s  %-10s  %-10s\n\n",
  109.                  "pubid", "date", "price", "avg(price)",
  110.                  "sales", "avg(sales)", "sum(sales)");
  111.  
  112.             /*
  113.             ** Print out each result row, using different statements
  114.             ** depending on whether the row is a regular row or a 
  115.             ** compute row.
  116.             */
  117.  
  118.             while ((row_code = dbnextrow(dbproc)) != NO_MORE_ROWS)
  119.             {
  120.                 if (row_code == REG_ROW)
  121.                 {
  122.                     printf
  123.                         ("%5s  %19s  %6s  %10s  %5ld  %10ld  %10ld\n",
  124.                          pubid, date, price, priceavg, sales, 
  125.                          salesavg, sumsale);
  126.                 }
  127.                 else
  128.                     printf("title count:  %ld\n\n",pcount); 
  129.             }
  130.         }
  131.     }
  132.  
  133.     dbexit(  );
  134.     exit(STDEXIT);
  135. }
  136.  
  137. int err_handler(dbproc, severity, dberr, oserr, dberrstr, oserrstr)
  138. DBPROCESS       *dbproc;
  139. int             severity;
  140. int             dberr;
  141. int             oserr;
  142. char            *dberrstr;
  143. char            *oserrstr;
  144. {
  145.     if ((dbproc == NULL) || (DBDEAD(dbproc)))
  146.         return(INT_EXIT);
  147.     else 
  148.     {
  149.         printf("DB-Library error:\n\t%s\n", dberrstr);
  150.  
  151.         if (oserr != DBNOERR)
  152.             printf("Operating-system error:\n\t%s\n", oserrstr);
  153.  
  154.         return(INT_CANCEL);
  155.     }
  156. }
  157.  
  158. int msg_handler(dbproc, msgno, msgstate, severity, msgtext, 
  159.                 srvname, procname, line)
  160.  
  161. DBPROCESS       *dbproc;
  162. DBINT           msgno;
  163. int             msgstate;
  164. int             severity;
  165. char            *msgtext;
  166. char            *srvname;
  167. char            *procname;
  168. DBUSMALLINT     line;
  169.  
  170. {
  171.     printf ("Msg %ld, Level %d, State %d\n", 
  172.             msgno, severity, msgstate);
  173.  
  174.     if (strlen(srvname) > 0)
  175.         printf ("Server '%s', ", srvname);
  176.     if (strlen(procname) > 0)
  177.         printf ("Procedure '%s', ", procname);
  178.     if (line > 0)
  179.         printf ("Line %d", line);
  180.  
  181.     printf("\n\t%s\n", msgtext);
  182.  
  183.     return(0);
  184. }
  185.